const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

// 每日一句表
const DailySentence = sequelize.define('daily_sentences', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "每日一句ID"
  },
  date: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    unique: true,
    comment: "日期"
  },
  chinese: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: "中文内容"
  },
  pinyin: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "拼音标注"
  },
  english: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "英文翻译"
  },
  source: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: "来源"
  },
  audio_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "音频URL"
  },
  difficulty: {
    type: DataTypes.ENUM('easy', 'medium', 'hard'),
    defaultValue: 'medium',
    comment: "难度级别"
  },
  tags: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "标签"
  },
  view_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "查看次数"
  },
  practice_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "练习次数"
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: "是否启用"
  }
}, {
  tableName: 'daily_sentences',
  indexes: [
    {
      unique: true,
      fields: ['date']
    },
    {
      fields: ['date']
    }
  ]
});

// 每日朗读记录表
const DailyRecord = sequelize.define('daily_records', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "每日记录ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  sentence_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "句子ID"
  },
  score: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "得分"
  },
  accuracy: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "准确度"
  },
  fluency: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "流利度"
  },
  completeness: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 0.00,
    comment: "完整度"
  },
  level: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: "评级"
  },
  video_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "视频URL"
  },
  suggestions: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "建议"
  },
  duration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "录制时长(秒)"
  }
}, {
  tableName: 'daily_records',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['sentence_id']
    }
  ]
});

module.exports = {
  DailySentence,
  DailyRecord
};
